package com.example.asus.kamr;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Karm extends AppCompatActivity {
private ImageView ivdog;
private Button btnNext;
private int[] dogImges={
        R.drawable.dog1, R.drawable.dog2, R.drawable.dog3, R.drawable.dog4, R.drawable.dog5
};
private int index=0;
private Handler handler;
private final int Message_change_Dog=1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_karm);
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what==Message_change_Dog){
                    index = (index + 1) % dogImges.length;
                    ivdog.setImageResource(dogImges[index]);
                    return true;
                }
                return false;
            }
        });
        ivdog=findViewById(R.id.img_dog);
        btnNext=findViewById(R.id.btn_next);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true){
                            try {
                                Thread.sleep(2000);
                            }
                            catch (InterruptedException e){
                                e.printStackTrace();
                            }
                            Message message=new Message();
                            message.what=Message_change_Dog;
                            handler.sendMessage(message);
                            Log.i("mainAtivity","send massage");
                        }
                    }
                }).start();
            }
        });
    }
}
